package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.config.Calibration;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.ParityFailureException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.service.SetmealService;
import com.sky.service.SetmealWithDishService;
import com.sky.service.setmeal_dishService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class SetmealWithDishServiceImpl implements SetmealWithDishService {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private setmeal_dishService setmealDishService;
    @Autowired
    private DishService dishService;

    /**
     * 新增套餐
     *
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void saveSetmeal(SetmealDTO setmealDTO) {
        //1.校验c层传进来的参数
        Calibration.addedSetmealVerification(setmealDTO);
        log.info("新增套餐：{}", setmealDTO);
        //2,将传入数据拷贝到实体类中
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //3.保存套餐数据
        setmealService.saveSetmeal(setmeal);
        //4.查询生成的套餐id
        Long id = setmeal.getId();
        log.info("套餐id为：{}", id);
        //5.判断id是否为空
        if (Objects.isNull(id)) {
            throw new ParityFailureException("套餐id为空");
        }
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(id);
        }
        //6.保存套餐和菜品的关联关系
        setmealDishService.saveSetmealDish(setmealDishes);
    }

    /**
     * 分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("套餐分页查询，参数：{}", setmealPageQueryDTO);
        //1,校验参数
        Calibration.pageQuery(setmealPageQueryDTO);
        //2,查询数据，封装返回
        return setmealService.pageQuery(setmealPageQueryDTO);
    }

    /**
     * 修改套餐
     *
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void updateWithFavorite(SetmealDTO setmealDTO) {
        log.info("修改套餐：{}", setmealDTO);
        //1.校验参数
        Calibration.addedSetmealVerification(setmealDTO);
        //2.修改套餐数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealService.update(setmeal);
        //3.删除套餐和菜品的关联关系
        Long id = setmealDTO.getId();
        if (Objects.isNull(id)) {
            throw new ParityFailureException("套餐id为空");
        }
        setmealDishService.deleteByIds(id);
        //4.拿到前端传进来的套餐和菜品关联关系
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(id);
        }
        //5.保存套餐和菜品的关联关系
        setmealDishService.saveSetmealDish(setmealDishes);
    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
        log.info("根据id查询套餐：{}", id);
        //1.校验参数
        if (Objects.isNull(id)) {
            throw new ParityFailureException("参数异常为空");
        }
        //2.调用服务层
        Setmeal setmeal = setmealService.getById(id);
        //3.拿到setmealDish的id
        Long setmealId = setmeal.getId();
        //4.根据id查询套餐和菜品的关联关系
        SetmealDish setmealDish = setmealDishService.getSetmealIdsByDishId(setmealId);
        ArrayList<SetmealDish> setmealDish1 = new ArrayList<>();
        setmealDish1.addAll(Collections.singleton(setmealDish));
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(setmealDish1);
        return setmealVO;
    }

    /**
     * 起售停售套餐
     *
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        //1.校验参数
        if (Objects.isNull(id)) {
            throw new ParityFailureException("参数异常为空");
        }
        if (Objects.isNull(status)) {
            throw new ParityFailureException("参数异常为空");
        }
        //2.判断套餐是起售还是停售
        if (status == StatusConstant.ENABLE) {
            //起售
            log.info("起售套餐：{}", id);
            //1.判断套餐内的菜品是否有停售的菜品
            List<Dish> bySetmealId = dishService.getBySetmealId(id);
            for (Dish dish : bySetmealId) {
                Integer status1 = dish.getStatus();
                if (status1 == StatusConstant.DISABLE) {
                    //有停售的菜品，抛出异常
                    throw new ParityFailureException("套餐内包含停售菜品，不能起售");
                }
            }
            setmealService.startOrStop(status, id);
        }
        setmealService.startOrStop(status, id);

    }

    /**
     * 批量删除套餐
     *
     * @param ids
     */
    @Override
    public void deleteByIds(List<Long> ids) {
        log.info("批量删除套餐：{}", ids);
        //1.校验参数
        if (Objects.isNull(ids)) {
            throw new ParityFailureException("参数异常为空");
        }
        //2.判断套餐是否起售状态
        for (Long id : ids) {
            Setmeal setmeal = setmealService.getById(id);
            Integer status = setmeal.getStatus();
            if (status == StatusConstant.ENABLE) {
                //起售状态，抛出异常
                throw new ParityFailureException("起售状态的套餐不能删除");
            }
            setmealService.deleteByIds(ids);
        }

        //3.删除套餐和菜品的关联关系
        for (Long id : ids) {
            setmealDishService.deleteByIds(id);
        }
    }
}
